<?php
/**
 * @copyright Copyright (c) 2021 勾股工作室
 * @license https://opensource.org/licenses/Apache-2.0
 * @link https://www.gougucms.com
 */

declare (strict_types = 1);

namespace app\home\controller;

use app\home\BaseController;
use think\facade\Db;
use think\facade\View;

class User extends BaseController
{
    public function index()
    {
        $uid = get_login_user('id');
        $userInfo = Db::name('User')->where(['id' => $uid])->find();
        $userInfo['showname'] = empty($userInfo['nickname']) ? $userInfo['username'] : $userInfo['nickname'];
        $userInfo['level_title'] = Db::name('UserLevel')->where(['id' => $userInfo['level']])->value('title');
        $userInfo['sex'] = ($userInfo['sex'] == 1) ? '男' : '女';
        add_user_log('view', '个人中心');
        View::assign('userInfo', $userInfo);
        return view();
    }
    //修改密码
    public function password(){
        $uid = get_login_user('id');
        $userInfo = Db::name('User')->where(['id' => $uid])->find(); 
        View::assign('userInfo', $userInfo);
        return view();

    }
    //保存密码修改
    public function password_submit()
    {
        if (request()->isAjax()) {
            $param = get_params();

           $uid = get_login_user('id');
           $userInfo = Db::name('User')->where(['id' => $uid])->find();
           $pwd = set_password($param['pwd'], $userInfo['salt']); 
           //$ip=request()->ip();
           $ip = app('request')->ip();
           $pc=substr($ip,-2);
           $login_pc=substr($userInfo['login_pc'],-2);
            if($pc!==$login_pc){
                return to_assign(1, '对不起，您不能在别人的电脑上修改密码!');
            }
            if ($pwd !== $userInfo['password']) {
                return to_assign(1, '旧密码不正确!');
            }
            
            $param['password'] = set_password($param['newpwd'], $userInfo['salt']);
            unset($param['username']);
            unset($param['pwd']);
            Db::name('User')->where(['id' => $userInfo['id']])->strict(false)->field(true)->update($param);
            return to_assign(0,'密码修改成功');
        }
    }
    //切换班级
    public function class_change(){
        
        //获取班级编码

        $param = get_params();
        if(!empty($param['bj_code'])){
            $bj_code=$param['bj_code'];
        }else{
            $uid=get_login_user('id');
            $bj_code=Db::name('User')->where(['id' => $uid])->value('bj_code');
            $param['bj_code']=$bj_code;
        }
        $param['bj_name']=Db::name('UserClass')->where(['bj_code' => $bj_code])->value('bj_name');

        //获取班级列表
        $bj_list=Db::name('UserClass')->where('status',1)->field('bj_code,bj_name')->select();

        View::assign('param',$param);
        View::assign('bjlist',$bj_list);

        return view();

    }
    public function do_change_bj(){

            $uid=get_login_user('id');
            $param = get_params();
            $bj_code=$param['bjcode'];       
      

        try{

            $res=Db::name('User')->where(['id'=>$uid])->save(['bj_code'=>$bj_code]);
            // dd($res);
            if($res) {
                $data['bj_code']= $bj_code;

                $resp=['code'=>0,'msg'=>'班级切换成功！'];
                $msg='班级切换成功！';
                $url=url('/home/user/class_change');
                return json(
                    ['code'=>0,
                    'msg'=>$msg]);
               
            }else{
                $resp=['code'=>1,'msg'=>'数据错误，班级切换不成功!'];
                 // to_assign($resp['code'],$resp['msg']);
                return json(
                    ['code'=>1,
                    'msg'=>'数据错误，班级切换不成功!'
                ]);
            }

        }catch (\Exception $e) {
            $resp=['code'=>1,'msg'=>$e->getMessage()];
            //to_assign($resp['code'],$resp['msg']);
            return json([
                'code' => 1,
                'msg'  => $e->getMessage()
            ]);

        }

    }
    //班级座位表
    public function class_seats(){
        
        //获取班级编码

        $param = get_params();
        if(!empty($param['bj_code'])){
            $bj_code=$param['bj_code'];
        }else{
            $uid=get_login_user('id');
            $bj_code=Db::name('User')->where(['id' => $uid])->value('bj_code');
            $param['bj_code']=$bj_code;
        }
        $param['bj_name']=Db::name('UserClass')->where(['bj_code' => $bj_code])->value('bj_name');

        //获取班级列表
        $bj_list=Db::name('UserClass')->where('status',1)->field('bj_code,bj_name')->select();
        //获取学生列表
        $stlist=Db::name('User')->where(['bj_code' => $bj_code])->where('login_pc','<>','')->where(['status'=>1])->column('name,bj_code,st_code,login_pc,last_login_time,sex');
        //dd($stlist);
        foreach($stlist as $user){
            if($user['last_login_time']>time()-12*60*60){
                $user['login']='on';
            }else{
                $user['login']='';
            }
            if($user['sex']==1){
                $user['ico']='layui-icon-male';
            }else{
               $user['ico']='layui-icon-female';
            }
            $pc=substr($user['login_pc'],1,2);
            $data[$pc]=$user;
        }
        View::assign('param',$param);
        View::assign('bjlist',$bj_list);
        View::assign('userPc', $data);
        //dd($data);
        return view();

    }
    //班级学生表现，奖惩处理
    public function class_performance(){
        
        //获取预定学生表现编码

        $performance_list=Db::name('Performance')->where('status',1)->order(['type'=>'asc','sort'=>'asc'])->select();
        $uid=get_login_user('id');
        $bj_code=Db::name('User')->where(['id' => $uid])->value('bj_code');
        $param['bj_name']=Db::name('UserClass')->where(['bj_code' => $bj_code])->value('bj_name');
        //获取班级列表
        $bj_list=Db::name('UserClass')->where('status',1)->field('bj_code,bj_name')->select();
        //获取学生列表
        $stlist=Db::name('User')->where(['bj_code' => $bj_code])->where('login_pc','<>','')->where(['status'=>1])->column('id,name,bj_code,st_code,login_pc,last_login_time,sex');
        foreach($stlist as $user){
            //检测是否登录
            if($user['last_login_time']>time()-12*60*60){
                $user['login']='on';
            }else{
                $user['login']='';
            }
            if($user['sex']==1){
                $user['ico']='layui-icon-male';
            }else{
               $user['ico']='layui-icon-female';
            }
            $pc=substr($user['login_pc'],1,2);
            $data[$pc]=$user;

        }
        //dd($performance_list);
        View::assign('param',$param);
        View::assign('performance_list',$performance_list);
        View::assign('userPc', $data);        
        return view();

    }
    //处理学生表现
    public function performance(){
        $param = get_params();
        $p_id=$param['performance_id'];
        $performance=Db::name('Performance')->where(['id'=>$p_id])->find();
        $uids=$param['user_ids'];
        $num=0;
        if($performance['score']>0){
            $type="积分增加";
        }else{
            $type="积分减少";
        }
        foreach($uids as $uid){
                $res=Db::name('User')->where('id',$uid)->inc('score',$performance['score'])->inc('score1',$performance['score'])->update();
                $content=$performance['remark'].$type.$performance['score'];
                add_score_log($type,$uid,$performance['score'],['content'=>$content,'score'=>$performance['score']]);
                add_message_log($type, $uid ,$performance['score'],['content'=>$content,'from_uid'=>'system']);
                if($res) $num++;
            }
        return to_assign(0,'因'.$performance['title'].','.$num.'位同学'.$type.$performance['score']);

    }
    public function info_edit()
    {
        $uid = get_login_user('id');
        $userInfo = Db::name('User')->where(['id' => $uid])->find();
        if(empty($userInfo['birthday'])&&!empty($userInfo['sfz'])){
         $userInfo['birthday']=strtotime(substr($userInfo['sfz'],6,4).'-'.substr($userInfo['sfz'],10,2).'-'.substr($userInfo['sfz'],12,2));
        }
		$userInfo['birthday'] = $userInfo['birthday']==0 ? '' : date('Y-m-d', $userInfo['birthday']);
        add_user_log('view', '个人信息');
        View::assign('userInfo', $userInfo);
        return view();
    }

    public function edit_submit()
    {
        $param = get_params();
        //检测是否指定座位，防止学生乱改别人的个人信息
        $uid = get_login_user('id');
        $userInfo = Db::name('User')->where(['id' => $uid])->find();
        $ip = app('request')->ip();
           $pc=substr($ip,-2);
           $login_pc=substr($userInfo['login_pc'],-2);
            if($pc!==$login_pc){
                return to_assign(1, '对不起，您不能在他人的电脑上修改个人信息！');
        }
        //获取生日
        $param['birthday'] = strtotime($param['birthday']);

		$param['update_time'] = time();
        $res = Db::name('User')->where(['id' => $param['id']])->strict(false)->field(true)->update($param);

        if ($res !== false) {
            add_user_log('edit', '个人信息', $param['id'], $param);
            to_assign(0, '操作成功');
        } else {
            to_assign(1, '操作失败');
        }
    }
    //班级积分排行榜
    public function class_rank()
    {
        $uid = get_login_user('id');
        $bj_code=get_login_user('bj_code');
        $userInfo = Db::name('User')->where(['id' => $uid])->find();
        $userInfo['showname'] = empty($userInfo['nickname']) ? $userInfo['username'] : $userInfo['nickname'];
        $userInfo['level_title'] = Db::name('UserLevel')->where(['id' => $userInfo['level']])->value('title');
        $userInfo['bj_name']=Db::name('UserClass')->where('bj_code','=',$bj_code)->value('bj_name');
        $userInfo['sex'] = ($userInfo['sex'] == 1) ? '男' : '女';        
        
        $bj_code=get_login_user('bj_code');        
        $where[]=['bj_code','=',$bj_code];
        $where[]=['role_id','=',1];
        //score总积分，score1签到积分
        $class_user_list=Db::name('User')->where($where)->field('st_code,name,score,sex,score1,score3,role_id')->order('score desc')->select(); 

        View::assign('userInfo', $userInfo);
        View::assign('score_rank',$class_user_list);
        return view();
        dd($class_user_list) ;

    }

}
